package xian.woniu.web.controller;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import xian.woniu.config.MinioProperties;
import xian.woniu.util.ResultVO;

import java.util.UUID;

/**
 * @author SongHang
 * @time 2022/07/01 16:00:26
 */
@Slf4j
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;

    @RequestMapping("dcertificate")
    public ResultVO upload(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            int lastDos = originalFilename.lastIndexOf(".");
            String ext = originalFilename.substring(lastDos);
            String newName = UUID.randomUUID().toString().replace("-", "") + ext;
            minioClient.putObject("sun", newName, file.getInputStream(), file.getContentType());
            //获取url
            String endpoint = minioProperties.getEndpoint();
            String finallUrl = endpoint +"sun/"+ newName;
            log.info(finallUrl+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            //往库里存照片地址由前端另起一个异步处理
            return new ResultVO(200, "upload success", finallUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(500, "upload failure");
        }
    }
}